import { Toast } from "vant";
export default {
  data() {
    return {
      checked: false,
      powerChecked: false,
      showSearch: false,
      nickname: "",
      form: {
        cstore_id: null,
        uid: null, //会员ID
        mobile: "",
        name: "",
        owner_auth: 0,
        status: 0
      }
    };
  },
  activated() {
    this.form.cstore_id = Number(this.$route.query.storeId);
    this.form.id = Number(this.$route.query.itemId);
    this.verifierDetail();
  },
  methods: {
    modelInput(event) {
      // const item = event.currentTarget.dataset.item,
      //   itemValue = event.detail.value;
      // this.setData({
      //   [item]: itemValue
      // });
    },
    initData() {
      let query = {
        cstore_id: null,
        uid: null, //会员ID
        mobile: "",
        name: "",
        owner_auth: 0,
        status: 0
      };
      this.form = query;
      this.nickname = "";
      this.checked = false;
      this.powerChecked = false;
      this.showSearch = false;
    },
    searchTransfer(val) {
      let reSearch = val.replace(/\D/g, "");
      // 防抖
      if (this.timer) clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        this.debounce(this.dividendSearch(reSearch));
      }, 500);
    },
    dividendSearch(reSearch) {
      if (reSearch) {
        this.nickname = "";
        $http.get("plugin.love.Frontend.Controllers.team-dividend-search.index", { search: reSearch }, "正在获取").then(
          response => {
            if (response.result == 1) {
              let res = response.data;
              this.nickname = res[0].nickname;
              this.showSearch = true;
            } else {
              this.nickname = "";
              this.showSearch = true;
              // this.$dialog.alert({ message: response.msg });
            }
          },
          function(response) {
            // console.log(response);
            // error callback
          }
        );
      }
    },
    verifierChange(event) {
      this.form.status = event ? 1 : 0;
    },
    powerChange(detail) {
      this.form.owner_auth = detail ? 1 : 0;
    },
    verifierDetail() {
      let json = {
        cstore_id: this.form.cstore_id,
        id: this.form.id
      };
      $http.post("plugin.coupon-store.frontend.clerk-manage.clerkDetail", json, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            let query = this.form;
            ({
              //解构赋值
              uid: query.uid,
              mobile: query.mobile,
              name: query.name,
              owner_auth: query.owner_auth,
              status: query.status
            } = res);
            this.checked = query.status === 1 ? true : false;
            this.powerChecked = query.owner_auth === 1 ? true : false;
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    verifierBtn() {
      if (this.form.id) {
        if (this.fun.isTextEmpty(this.nickname) && this.showSearch) {
          //修改判断是否有会员
          Toast("未找到会员");
          return;
        }
      } else {
        if (this.fun.isTextEmpty(this.nickname) && this.fun.isTextEmpty(this.form.id)) {
          //判断是否有会员
          Toast("未找到会员");
          return;
        }
      }
      $http.post("plugin.coupon-store.frontend.clerk-manage.changeClerk", this.form, "正在获取").then(
        response => {
          if (response.result == 1) {
            // let res = response.data;
            this.initData();
            setTimeout(() => {
              this.$router.go(-1);
            }, 500);
            Toast(response.msg);
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    debounce(fn, delay) {
      let last, deferTimer;
      return function(args) {
        let that = this;
        let _args = arguments;
        let now = +new Date();
        if (last && now < last + delay) {
          clearTimeout(deferTimer);
          deferTimer = setTimeout(function() {
            last = now;
            fn.apply(that, _args);
          }, delay);
        } else {
          last = now;
          fn.apply(that, _args);
        }
      };
    }
  }
};
